package com.pr.makfilm.user.pages.room.details.cmp;

import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.domain.Room;
import com.pr.makfilm.service.IRoomService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * Created with IntelliJ IDEA.
 * User: Barbara
 * Date: 27.10.13
 * Time: 01:47
 * To change this template use File | Settings | File Templates.
 */
public class RoomDataTab extends Panel
{
   @SpringBean
   private IRoomService roomService;

   public RoomDataTab(String id, IModel<Room> model)
   {
      super(id, model);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      add(new Label("name", new PropertyModel<String>(getDefaultModel(), Room.FIELD_NAME)));
      add(new EditLabel<String>("tax.id", new PropertyModel<String>(getDefaultModel(), Room.FIELD_TAX_ID))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("capacity", new PropertyModel<String>(getDefaultModel(), Room.FIELD_CAPACITY))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("price", new PropertyModel<String>(getDefaultModel(), Room.FIELD_LEASE_PRICE))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("first.name", new PropertyModel<String>(getDefaultModel(), Room.FIELD_CONTACT_FIRST_NAME))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("last.name", new PropertyModel<String>(getDefaultModel(), Room.FIELD_CONTACT_LAST_NAME))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("phone", new PropertyModel<String>(getDefaultModel(), Room.FIELD_PHONE))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("mobile", new PropertyModel<String>(getDefaultModel(), Room.FIELD_MOBILE))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("email", new PropertyModel<String>(getDefaultModel(), Room.FIELD_EMAIL))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("street", new PropertyModel<String>(getDefaultModel(), Room.FIELD_STREET))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("zip", new PropertyModel<String>(getDefaultModel(), Room.FIELD_ZIP))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
      add(new EditLabel<String>("city", new PropertyModel<String>(getDefaultModel(), Room.FIELD_CITY))
      {
         @Override
         protected void onSubmit(AjaxRequestTarget target)
         {
            super.onSubmit(target);
            submit();
         }
      });
   }

   protected void submit()
   {
      Room room = (Room) getDefaultModelObject();
      try
      {
         room = roomService.update(room);
         Notification.success(getString("update.success"));
         setDefaultModelObject(room);
      }
      catch (Exception e)
      {
         Notification.error(e.getMessage());
      }
   }
}
